Slip 12


Q.1. Write a python program to implement k-nearest Neighbors ML algorithm to build 
prediction model (Use iris Dataset).

# Import necessary libraries
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

# Step 1: Load the Iris dataset
iris = load_iris()
X = iris.data      # Features: sepal length, sepal width, petal length, petal width
y = iris.target    # Target: 0 - setosa, 1 - versicolor, 2 - virginica

# Step 2: Convert into a pandas DataFrame (optional, for visualization)
df = pd.DataFrame(X, columns=iris.feature_names)
df['Target'] = y
print("Iris Dataset (first 5 rows):\n", df.head(), "\n")

# Step 3: Split dataset into training and testing parts (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Step 4: Create and train the KNN model
k = 3  # You can change this value for experimentation
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)

# Step 5: Predict on test data
y_pred = knn.predict(X_test)

# Step 6: Evaluate model performance
print("Predictions:", y_pred)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))

# Step 7: Example prediction
sample = [[5.1, 3.5, 1.4, 0.2]]  # Sepal length, Sepal width, Petal length, Petal width
predicted_class = knn.predict(sample)
print("\nExample Input:", sample)
print("Predicted Class:", iris.target_names[predicted_class][0])


Q.2 Fit the simple linear regression and polynomial linear regression models to Salary_positions.csv data. Find which one is more accurately fitting to the given data. Also predict the salaries of level 11 and level 12 employees

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import r2_score

# Step 1: Load the dataset
dataset = pd.read_csv('Salary_positions.csv')
X = dataset[['Level']]     # Feature: Position level (as DataFrame)
y = dataset['Salary']      # Target: Salary

# Step 2: Simple Linear Regression
lin_reg = LinearRegression()
lin_reg.fit(X, y)

# Step 3: Polynomial Regression (degree 2)
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)

lin_reg_poly = LinearRegression()
lin_reg_poly.fit(X_poly, y)

#Step 4: Visualize Simple Linear Regression
plt.scatter(X, y, color='red')
plt.plot(X, lin_reg.predict(X), color='blue')
plt.title('Simple Linear Regression')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.show()

#Step 5: Visualize Polynomial Regression
X_grid = np.arange(min(X['Level']), max(X['Level']) + 0.1, 0.1).reshape(-1, 1)
X_grid_df = pd.DataFrame(X_grid, columns=['Level'])

plt.scatter(X, y, color='red')
plt.plot(X_grid, lin_reg_poly.predict(poly.transform(X_grid_df)), color='green')
plt.title('Polynomial Regression (Degree 2)')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.show()

#Step 6: Accuracy Comparison
y_pred_linear = lin_reg.predict(X)
y_pred_poly = lin_reg_poly.predict(X_poly)

r2_linear = r2_score(y, y_pred_linear)
r2_poly = r2_score(y, y_pred_poly)

print(f"R² Score (Linear Regression): {r2_linear:.4f}")
print(f"R² Score (Polynomial Regression): {r2_poly:.4f}")

#Step 7: Predictions for Level 11 and 12 
level_11 = pd.DataFrame([[11]], columns=['Level'])   
level_12 = pd.DataFrame([[12]], columns=['Level'])   

linear_pred_11 = lin_reg.predict(level_11)
linear_pred_12 = lin_reg.predict(level_12)

poly_pred_11 = lin_reg_poly.predict(poly.transform(level_11)) 
poly_pred_12 = lin_reg_poly.predict(poly.transform(level_12))  

print("\nPredicted Salaries:")
print(f"Linear - Level 11: ₹{linear_pred_11[0]:,.2f}")
print(f"Linear - Level 12: ₹{linear_pred_12[0]:,.2f}")
print(f"Polynomial - Level 11: ₹{poly_pred_11[0]:,.2f}")
print(f"Polynomial - Level 12: ₹{poly_pred_12[0]:,.2f}")
